package main

import (
	"GrpcProj/dbtools"
	pb "GrpcProj/grpc"
	"GrpcProj/test"
	"fmt"
	_ "github.com/jinzhu/gorm/dialects/mysql"
	"golang.org/x/net/context"
	"google.golang.org/grpc"
	"google.golang.org/grpc/reflection"
	"log"
	"net"
	"os"
)

type server struct {

}

type Student struct{
	Stu_id uint `gorm:"primary_key"`
	Stu_name string
	Stu_age int32
	Stu_sex string `gorm:"size:20"`
}

const (
	port = ":50051"
)

func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
	fmt.Println("######### get client request name :"+in.Name)
	db := dbtools.GetInstance().GetMysqlDB()
	student := Student{}
	db.Where("stu_name = ?", in.Name).First(&student)
	return &pb.HelloReply{Name: student.Stu_name, Age: student.Stu_age, Sex: student.Stu_sex}, nil
}


func main(){
	fmt.Println(test.Hello())
	issucc := dbtools.GetInstance().InitDataPool()
	if !issucc {
		log.Println("init database pool failure...")
		os.Exit(1)
	}

	lis, err := net.Listen("tcp", port)
	if err != nil {
		log.Fatalf("failed to listen: %v", err)
	}
	s := grpc.NewServer()
	pb.RegisterGreeterServer(s, &server{})
	reflection.Register(s)
	if err := s.Serve(lis); err != nil{
		log.Fatalf("failed to serve: %v", err)
	}
}
